package com.ruoyi.custom.strategy.wechatmp;

import com.ruoyi.common.enums.WxMpMsgType;
import com.ruoyi.common.utils.bean.SpringContextUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息处理策略工厂
 *
 * @author xudm
 * @description TODO
 * @date 2024-09-27  16:31
 */
public class WechatMpMessageHandleStrategyFactory {

    private static final Map<WxMpMsgType, Class<? extends WechatMpMessageHandleStrategy>> strategyMap = new HashMap<>();

    static {
        strategyMap.put(WxMpMsgType.TEXT, TextMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.IMAGE, ImageMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.VOICE, VoiceMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.VIDEO, VideoMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.SHORTVIDEO, ShortVideoMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.LOCATION, LocationMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.LINK, LinkMessageHandleStrategy.class);
        strategyMap.put(WxMpMsgType.EVENT, EventMessageHandleStrategy.class);
    }

    public static WechatMpMessageHandleStrategy getMessageHandleStrategy(WxMpMsgType msgType) {
        WechatMpMessageHandleStrategy strategy = null;
        // 根据消息类型返回对应的处理策略
        if (msgType == null) {
            return null;
        }
        Class<? extends WechatMpMessageHandleStrategy> strategyClass = strategyMap.get(msgType);
        if (strategyClass == null) {
            throw new IllegalArgumentException("No strategy found for message type: " + msgType);
        }
        strategy = SpringContextUtil.getBean(strategyClass);
        return strategy;
    }
}
